//
//  HDCFHDMMoneyVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/2.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFHDMMoneyVc.h"
#import "HDCFDdmmCell.h"
#import "SVProgressHUD.h"
#import "MJRefresh.h"
#import "HDCFBuyFundVc.h"
#import "HDCFFundDetailVc.h"
#import "HDCFFundSubscribeDetailVC.h"//基金预约内基金详情

#define KGatherFunder @"/public/product/collective/find-collective-info-list"

static NSString * cellID = @"IdentifierOne";
@interface HDCFHDMMoneyVc ()<UITableViewDelegate, UITableViewDataSource, HDCFDdmmCellDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *bannerView;
@property (weak, nonatomic) IBOutlet UITableView *fundTableView;
@property (nonatomic, strong) NSMutableArray *fundArray;
@property (nonatomic, assign) NSInteger page;
@end

@implementation HDCFHDMMoneyVc

- (NSMutableArray *)fundArray{
    if (!_fundArray) {
        self.fundArray = [NSMutableArray array];
    }
    return _fundArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_fromID == 1) {
        [self.navigationItem setTitle:@"好得理财"];
        [self initData];
    }
    else if (_fromID ==2)
    {
        [self.navigationItem setTitle:@"基金预约"];
        [self initDataOfFund];
    }
    self.fundTableView.delegate = self;
    self.fundTableView.dataSource = self;
    self.fundTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.fundTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //[self.fundTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellID];
    
    self.page = 1;
    self.fundTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(mainRefresh)];
    self.fundTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(mainLoadMore)];
    
}

- (void)mainRefresh{
    [self.fundArray removeAllObjects];
    if (_fromID ==1) {
       [self initDataTwo];
    }
    else if(_fromID == 2)
    {
        [self initDataOfFund];
    }
}

- (void)mainLoadMore{
    self.page++;
    if (_fromID ==1) {
        [self initDataTwo];
    }
    else if(_fromID == 2)
    {
        [self initDataOfFund];
    }
}
#pragma mark -
#pragma mark RequestData
- (void)initData{
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"pageSize"] = @"10";
    paramets[@"pageIndex"] = [NSString stringWithFormat:@"%ld", (long)self.page];
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{KparametOne:jsonStr,
                              KparametTwo:KGatherFunder};
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            NSDictionary *dataDict = result[@"data"];
            [SVProgressHUD dismiss];
            NSArray *dataRraty = dataDict[@"data"];
            if (dataRraty.count == 10) {
                [self.fundTableView.mj_footer endRefreshing];
            }else{
                [self.fundTableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.fundArray addObjectsFromArray:dataDict[@"data"]];
            for (int i = 0; i < _fundArray.count; i++) {
                NSDictionary *dict = _fundArray[i];
                NSString *isBuyEnable = dict[@"isBuyEnable"];
                if ([isBuyEnable isEqualToString:@"Y"]) {
                    [_fundArray removeObject:dict];
                    [_fundArray insertObject:dict atIndex:0];
                }
            }
            
            [self.fundTableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD  dismissWithDelay:1.0];
            [self.fundTableView.mj_footer endRefreshing];
            [self.fundTableView.mj_header endRefreshing];
        }
        
    } failure:^(NSError *errer) {
        [self.fundTableView.mj_footer endRefreshing];
        [self.fundTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD  dismissWithDelay:1.0];
    }];
}

- (void)initDataTwo{
   
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"pageSize"] = @"10";
    paramets[@"pageIndex"] = [NSString stringWithFormat:@"%ld", (long)self.page];
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{KparametOne:jsonStr,
                              KparametTwo:KGatherFunder};
    [HttpRequest sendGetRequestWithURL:nil WithParamets:paramet success:^(id result) {
        NSString *code = result[@"code"];
        if ([code isEqualToString:@"0000"]) {
            NSDictionary *dataDict = result[@"data"];
            
            NSArray *dataRraty = dataDict[@"data"];
            if (dataRraty.count == 10) {
                [self.fundTableView.mj_footer endRefreshing];
            }else{
                [self.fundTableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.fundArray addObjectsFromArray:dataDict[@"data"]];
            for (int i = 0; i < _fundArray.count; i++) {
                NSDictionary *dict = _fundArray[i];
                NSString *isBuyEnable = dict[@"isBuyEnable"];
                if ([isBuyEnable isEqualToString:@"Y"]) {
                    [_fundArray removeObject:dict];
                    [_fundArray insertObject:dict atIndex:0];
                }
            }
            [self.fundTableView reloadData];
            [self.fundTableView.mj_header endRefreshing];
        }else{
            [self.fundTableView.mj_footer endRefreshing];
            [self.fundTableView.mj_header endRefreshing];
        }
        
    } failure:^(NSError *errer) {
        [self.fundTableView.mj_footer endRefreshing];
        [self.fundTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}

-(void)initDataOfFund
{
    [SVProgressHUD show];
    NSMutableDictionary *paramets = [NSMutableDictionary dictionary];
    paramets[@"pageSize"] = @"10";
    paramets[@"pageIndex"] = [NSString stringWithFormat:@"%ld", (long)self.page];
    NSString *jsonStr = [self dictionaryToJson:paramets];
    NSDictionary *paramet = @{KparametOne:jsonStr,
                              KparametTwo:KGatherFunder};
    [HttpRequest sendSelfGetRequestWithURL:@"api/Product" WithParamets:paramet success:^(id result) {
        //LLLog(@"%@",result);
        [self.fundTableView.mj_header endRefreshing];
        [self.fundTableView.mj_footer endRefreshing];
        NSString *code = [result[@"code"] stringValue];
        if ([code isEqualToString:@"1"]) {
            [SVProgressHUD dismiss];
            NSArray * dataRraty = result[@"result"];
            if (dataRraty.count < 10) {
                [self.fundTableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            if (_page == 1) {
                [self.fundArray removeAllObjects];
            }
            
            [self.fundArray addObjectsFromArray:dataRraty];
            for (int i = 0; i < _fundArray.count; i++) {
                NSDictionary *dict = _fundArray[i];
                BOOL isHidden = [dict[@"Hidden"] boolValue];
                if (isHidden == NO) {
                    [_fundArray removeObject:dict];
                    [_fundArray insertObject:dict atIndex:i];
                }
            }
            [self.fundTableView reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:result[@"message"]];
            [SVProgressHUD  dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [self.fundTableView.mj_footer endRefreshing];
        [self.fundTableView.mj_header endRefreshing];
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        [SVProgressHUD  dismissWithDelay:1.0];
    }];
}

#pragma mark -
#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LLLog(@"数组的值为%@",self.fundArray);
    return self.fundArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HDCFDdmmCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [HDCFDdmmCell HDCFDdmmCell];
    }
    NSDictionary *dict = self.fundArray[indexPath.row];
    cell.index = indexPath.row;
    cell.fromID = _fromID;
    cell.dataDict = dict;
    cell.delegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.fundArray[indexPath.row];
    if (_fromID == 1) {
        NSString *isBuyEnable = dict[@"isBuyEnable"];
        if ([isBuyEnable isEqualToString:@"Y"])
        {
            HDCFFundDetailVc *vc = [[HDCFFundDetailVc alloc] init];
            vc.isLiCai = YES;
            vc.dictTwo = dict;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    else if (_fromID == 2)
    {
        NSInteger state = [dict[@"State"]integerValue];
        //state为1表示可以预约状态
        if (state == 1)
        {
            
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 202;
}

#pragma mark cell的代理方法
- (void)hdcfDdmmcell:(HDCFDdmmCell *)ddmmCell withIndex:(NSInteger)index{
    //好得理财
    if (_fromID == 1) {
        NSDictionary *dict = self.fundArray[index];
        HDCFFundDetailVc *vc = [[HDCFFundDetailVc alloc] init];
        vc.isLiCai = YES;
        vc.dictTwo = dict;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if (_fromID == 2)//基金预约
    {
        NSDictionary *dict = self.fundArray[index];
        HDCFFundSubscribeDetailVC * vc = [[HDCFFundSubscribeDetailVC alloc]init];
        vc.Id = [dict[@"Id"]integerValue];
        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


@end
